package com.lepu.app.main.service;

import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.app.utils.Setting;
import com.core.lib.core.AsyncRequest;
import com.core.lib.core.HttpRequest;
import com.core.lib.utils.Reachability;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.chat.activity.DoctorMainActivity;
import com.lepu.app.fun.chat.activity.DoctorMyActivity;
import com.lepu.app.fun.chat.bean.BeanDoctorMy;
import com.lepu.app.fun.chat.business.DoctorManager;
import com.lepu.app.fun.chat.util.SingManager;
import com.lepu.pasm.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AppService extends Service implements AsyncRequest {

	private static final int NOTIFY_TIME_1 = 1000 * 60 * 1;// 间隔1分钟
	public static final String REQUEST_CHECK_DOCTOR_CHANGE = "request_check_doctor_change";

	private static final int MSG_DOCTOR_CHANGE_SUCCESS = 10;
	private static final int MSG_DOCTOR_CHANGE_FAIL = 11;

	private static AppService mInstance = null;
	private NotificationManager mNotificationManager = null;
	private Timer mDoctorChangeTimer = null;

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		//startDoctorChangeTimer();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInstance = null;

		deleteNotification();
		//stopDoctorChangeTimer();
	}

	public void deleteNotification() {
		// 删除通知
		if (mNotificationManager != null) {
			//mNotificationManager.cancel(NOTIFY_ID_YUJIN_LIST);
		}
	}

	public static AppService getInstance() {
		return mInstance;
	}

	/*
	 * 开启app sevice
	 */
	public static void startAppService(Context context) {
		AppService appService = AppService.getInstance();
		if (appService == null) {
			Intent serviceIntent = new Intent(context, AppService.class);
			context.startService(serviceIntent);
		}
	}

	/*
	 * 停止app service
	 */
	public static void stopAppService(Context context) {
		AppService appService = AppService.getInstance();
		if (appService != null) {
			Intent serviceIntent = new Intent(context, AppService.class);
			context.stopService(serviceIntent);
		}
	}

	public void requestCheckDoctorChange() {
		Activity currentActivity = AppManager.getAppManager().currentActivity();
		if(currentActivity == null)
		{
			return;
		}

		if (Reachability.checkNetwork(mInstance) && !UtilityBase.isApplicationBackground(this)) {
			DoctorManager.getInstance().requestDoctorRelationHistory(this);
		}
	}

	public void startDoctorChangeTimer() {
		if (mDoctorChangeTimer == null) {
			mDoctorChangeTimer = new Timer();
			mDoctorChangeTimer.schedule(new TimerTask() {

				@Override
				public void run() {
					requestCheckDoctorChange();
				}
			}, 0, NOTIFY_TIME_1);
		}
	}

	public void stopDoctorChangeTimer() {
		if (mDoctorChangeTimer != null) {
			mDoctorChangeTimer.cancel();
			mDoctorChangeTimer.purge();
			mDoctorChangeTimer = null;
		}
	}

	@SuppressLint("HandlerLeak")
	public Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_DOCTOR_CHANGE_SUCCESS:

				SingManager.getInstance().setVoiceReminder();

				List<BeanDoctorMy> dataList = (List<BeanDoctorMy>) msg.obj;

				for(BeanDoctorMy item : dataList)
				{
					showDoctorTipDialog(item);
				}

				break;
			case MSG_DOCTOR_CHANGE_FAIL:

				break;
			default:
				break;
			}
		};
	};

	private void showDoctorTipDialog(BeanDoctorMy item)
	{
		Activity currentActivity = AppManager.getAppManager().currentActivity();

		if(currentActivity == null)
		{
			return ;
		}

		View view = LayoutInflater.from(this).inflate(
				R.layout.chat_doctor_relation_change, null);

		final MyDialog dialog = new MyDialog(currentActivity)
				.addView(view);

		CircularImageView accountIconImageView = (CircularImageView) view.findViewById(R.id.accountIconImageView);
		accountIconImageView
				.setImageResource(R.drawable.chat_doctor_icon_default);
		UniversalImageLoadTool.disPlay(
				Setting.getImageUrl(item.UniqueID),
				accountIconImageView, R.drawable.chat_doctor_icon_default);

		TextView nameTextView = (TextView) view.findViewById(R.id.nameTextView);
		Button cancelButton = (Button) view.findViewById(R.id.cancelButton);

		nameTextView.setText(item.DoctorName);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				dialog.dismiss();

				DoctorMyActivity instance = (DoctorMyActivity) AppManager.getAppManager().getActivity(DoctorMyActivity.class);
				if (instance != null)
				{
					instance.refreshData();
				}
			}
		});

		dialog.create(null);
		dialog.showMyDialog();
	}

	@Override
	public void RequestComplete(Object context, Object data) {
		if(context.equals(REQUEST_CHECK_DOCTOR_CHANGE))
		{
			List<BeanDoctorMy> dataList = BeanDoctorMy
					.parseListInfo((String) data);
			if (dataList != null && dataList.size() > 0) {
				DoctorManager.getInstance().saveDoctorsDB(dataList);

				Message msg = mHandler.obtainMessage();
				msg.obj = dataList;
				msg.what = MSG_DOCTOR_CHANGE_SUCCESS;
				mHandler.sendMessage(msg);
			}
		}
	}
	
	@Override
	public void RequestError(Object context, int errorId, String errorMessage) {
		if(context.equals(REQUEST_CHECK_DOCTOR_CHANGE))
		{
			mHandler.sendEmptyMessage(MSG_DOCTOR_CHANGE_FAIL);
		}
	}

}
